﻿using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OfficeHelper
{
	class Program
	{
		static void Main(string[] args)
		{
			test5();
			Console.ReadKey();
		}

		//static void test2()
		//{
		//	WordTemplateConfig tempConfig = WordHelper.GetConfig(@"test_template.docx");

		//	foreach(string k in tempConfig.DocumentObjectConfigs.Keys)
		//	{
		//		DocumentObjectConfig config = tempConfig.DocumentObjectConfigs[k];
		//		Console.WriteLine(string.Format("名称：{0}，类型：{1}, 路径：{2}", config.Name, config.DocumentObjectType, config.Path));
		//	}

		//	Dictionary<string, string> data = WordHelper.GetData(@"test_base.docx", tempConfig);

		//	foreach (var k in data)
		//	{
		//		Console.WriteLine(string.Format("名称：{0}，值：{1}", k.Key, k.Value));
		//	}

		//	WordHelper.RenderTemplate(@"test_rendered.docx", tempConfig, data);
		//}

		static void test3()
		{
			Document doc = new Document();
			doc.LoadFromFile(@"Listen2/test1.docx");

			var children = doc.Sections[0].Paragraphs[0].ChildObjects;
			var hasKey = false;
			var keyText = "";
			foreach(DocumentObject child in children)
			{
				if(child.DocumentObjectType == DocumentObjectType.TextRange)
				{
					TextRange textRange = child as TextRange;
					if (textRange.CharacterFormat.UnderlineStyle == UnderlineStyle.Single)
					{
						if (hasKey)
						{
							keyText += textRange.Text;
						}
						else
						{
							if(textRange.Text.First() == '$')
							{
								hasKey = true;
								keyText = textRange.Text;
							}
						}
					}
					else
					{
						if(hasKey)
						{
							Console.WriteLine(keyText);
						}
						hasKey = false;
					}
				}
			}
			if(hasKey)
			{
				Console.WriteLine(keyText);
			}


			foreach (DocumentObject child in children)
			{
				if (child.DocumentObjectType == DocumentObjectType.TextRange)
				{
					TextRange textRange = child as TextRange;
					if (textRange.CharacterFormat.UnderlineStyle == UnderlineStyle.Single)
					{
						if (hasKey)
						{
							textRange.Text = "";
						}
						else
						{
							if (textRange.Text.First() == '$')
							{
								hasKey = true;
								textRange.Text = "这是标题";
                                textRange.CharacterFormat.UnderlineStyle = UnderlineStyle.None;
							}
						}
					}
					else
					{
						hasKey = false;
					}
				}
			}
			doc.SaveToFile(@"Listen2/test1_1.docx");
		}

        static void test4()
        {
            //WordTemplateConfig config = WordHelper.GetConfig(@"BaseTemplate/template.docx");
            //Dictionary<string, string> data = WordHelper.GetData(@"Listen2/test1.docx" ,config);

            //WordTemplateConfig config2 = WordHelper.GetConfig(@"Template/template1.docx");
            //config2.BuiltInFuncs = new BuiltInFuncs1();
            //WordHelper.RenderTemplate(@"Output/template1_1.docx", config2, data);

            //Dictionary<string, string> replaceData = new Dictionary<string, string>();
            //replaceData.Add("s-title", "新的标题：");
            //WordHelper.ModifyTemplate(@"Template/template1.docx", @"Template/template2.docx", replaceData);
        }

        static void test5()
        {
            Document doc = new Document();
            doc.LoadFromFile("Listen2/test1.docx");

            Document doc2 = new Document();
            doc2.AddSection().AddParagraph().Text = "hahahah";

            doc.Replace("title", doc2, false, true);

            doc.SaveToFile("Listen2/test1_1.docx");
            Console.WriteLine("完成");
        }
	}
}
